package com.tuanzi.loan.business.service.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.CheckUserStatRepository;
import com.tuanzi.loan.business.service.statistics.CheckUserStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.check.CheckUserStatRequest;
import com.tuanzi.loan.vo.response.check.CheckUserStatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 撞库统计 业务层接口实现类
 *
 * created by chuIllusions_tan 20180727
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class CheckUserStatServiceImpl implements CheckUserStatService{

    @Autowired
    private CheckUserStatRepository checkUserStatRepository;

    @Override
    public Page<CheckUserStatResponse> list(PageSearcher<CheckUserStatRequest> pageSearcher) {
        if (Objects.isNull(pageSearcher))
            throw new NullPointerException("pageSearcher is null");
        if (Objects.isNull(pageSearcher.getSearchCondition()) || Objects.isNull(pageSearcher.getPageable()))
            throw new NullPointerException("activityparticipatestatSearchRequest or pageable is null");
        return checkUserStatRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public String getExcel(CheckUserStatRequest request) {
        List<CheckUserStatResponse> list = getDataList(request);
        StringBuilder sf = new StringBuilder();
        sf.append("时间,产品,渠道,平台,广告,活动,总撞库人次,成功人次,失败人次,撞库率\r\n");
        for (CheckUserStatResponse checkUserStatResponse: list) {
            sf.append(checkUserStatResponse.getTime()).append(",");
            sf.append(checkUserStatResponse.getProduct()).append(",");
            sf.append(checkUserStatResponse.getChannel()).append(",");
            sf.append(checkUserStatResponse.getPlatform()).append(",");
            sf.append(checkUserStatResponse.getIds()).append(",");
            sf.append(checkUserStatResponse.getActivity()).append(",");
            sf.append(checkUserStatResponse.getCheck_success()).append(",");
            sf.append("\r\n");
        }
        return sf.toString();
    }

    public List<CheckUserStatResponse> getDataList(CheckUserStatRequest searchRequest) {
        return checkUserStatRepository.getDataList(searchRequest);
    }
}
